using System;
using NUnit.Framework;
using Rhino.Mocks;

using ServiceManager;
using ServiceFacade;

namespace Test_ServiceManager
{
    [TestFixture]
    public class Test_Service
    {
        private Service service;
        private MockRepository mockRepository;
        private IServiceFacade mockServiceFacade;

        [SetUp]
        public void Setup()
        {
            this.service = new Service("Test_Service", ServiceStatus.Started);
            this.mockRepository = new MockRepository();
            this.mockServiceFacade = (IServiceFacade)this.mockRepository.DynamicMock(typeof(IServiceFacade),null);
         }

        [Test]
        public void Test_Service_Constructor()
        {
            Assert.AreEqual("Test_Service", this.service.ServiceName);
            Assert.AreEqual(ServiceStatus.Started, this.service.ServiceStatus);
        }

        [Test]
        public void Test_Service_SetServiceStatus()
        {
            this.service.ServiceStatus = ServiceStatus.Stopped;
            Assert.AreEqual(ServiceStatus.Stopped, this.service.ServiceStatus);
        }

        [Test]
        public void Test_Service_ExecuteCommand_StopCommandValidStatus()
        {
            using (this.mockRepository.Ordered())
            {
                this.mockServiceFacade.stopService("Test_Service");
            }
            this.mockRepository.ReplayAll();
            this.service.ExecuteCommand(CommandType.StopService, this.mockServiceFacade);
            this.mockRepository.VerifyAll();

            Assert.AreEqual(ServiceStatus.Stopped, this.service.ServiceStatus);
        }

        [Test]
        public void Test_Service_ExecuteCommand_StartCommandValidStatus()
        {
            this.service.ServiceStatus=ServiceStatus.Stopped;

            using (this.mockRepository.Ordered())
            {
                this.mockServiceFacade.startService("Test_Service");
            }
            this.mockRepository.ReplayAll();
            this.service.ExecuteCommand(CommandType.StartService, this.mockServiceFacade);
            this.mockRepository.VerifyAll();

            Assert.AreEqual(ServiceStatus.Started, this.service.ServiceStatus);
        }

        [Test, ExpectedExceptionAttribute(typeof(InvalidStatusException))]
        public void Test_Service_ExecuteCommand_StopCommandInvalidStatus()
        {
            this.service.ServiceStatus=ServiceStatus.Stopped ;
            this.service.ExecuteCommand(CommandType.StopService, this.mockServiceFacade);
        }

        [Test, ExpectedExceptionAttribute(typeof(InvalidStatusException))]
        public void Test_Service_ExecuteCommand_StartCommandInvalidStatus()
        {
            this.service.ServiceStatus=ServiceStatus.Started;
            this.service.ExecuteCommand(CommandType.StartService, this.mockServiceFacade);
        }


        [TearDown]
        public void TearDown()
        {
            this.mockServiceFacade = null;
            this.mockRepository = null;
            this.service = null;
        }
    }
}
